package com.zx.crowdfunding.mvc.handler;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zx.crowdfunding.entity.Auth;
import com.zx.crowdfunding.service.api.AuthService;
import com.zx.crowdfunding.util.ResultEntity;

/**
 * 权限 - Handler
 * @author 郑雪
 * @date 2021-12-09
 */
@RestController
public class AuthHandler {

	/**
	 * 权限Service接口
	 */
	@Autowired
	private AuthService authService;
	
	/**
	 * 查询权限树形结构
	 * @return JSON数据
	 */
	@RequestMapping("/auth/get/tree.json")
	public ResultEntity<List<Auth>> getTree(){
		
		// 1.查询全部的Auth对象
		List<Auth> list = authService.getAll();
		
		// 2.遍历list
		for (Auth auth : list) {
			
			// 打开权限
			auth.setOpen(true);
		}
		
		// 3.返回JSON数据
		return ResultEntity.successWithData(list);
	}
	
	/**
	 * 添加权限
	 * @param auth 权限对象
	 * @return JSON数据
	 */
	@RequestMapping("/auth/save.json")
	public ResultEntity<String> save(Auth auth){
		
		// 执行添加
		authService.saveAuth(auth);
		
		// 返回JSON数据
		return ResultEntity.successWithoutData();
	}
	
	/**
	 * 修改权限
	 * @param auth 权限对象
	 * @return JSON数据
	 */
	@RequestMapping("/auth/edit.json")
	public ResultEntity<String> edit(Auth auth){
		
		// 执行修改
		authService.editAuth(auth);
		
		// 返回JSON数据
		return ResultEntity.successWithoutData();
	}
	
	/**
	 * 删除权限
	 * @param idList 权限id的List集合
	 * @return JSON数据
	 */
	@RequestMapping("/auth/remove.json")
	public ResultEntity<String> remove(@RequestBody List<Integer> idList){
		
		// 执行删除
		authService.removeAuth(idList);
		
		// 返回JSON数据
		return ResultEntity.successWithoutData();
	}
}
